function PopupDNR(roomId,roomName,dnr,list){
	$("[name=dynamic]").load('/components/blocked/popup/DNR.jsp',function(responseText, textStatus, XMLHttpRequest) {
		if (textStatus == "success") {
			FillDNRRoomLabel();
			var title = cleaning_dnrRooms + " " + simpleLabel_room + " : " + roomName;
			var content = $("#DNRRoom").html();

			var buttons = [];
			if(dnr==null||(new Date(dnr.dateTo) > getDate()))
				buttons.push({ title:"Save", callback: function(thisdialog,args){
					thisdialog.dialog("close");
					if(dnr==null)SaveDNRRoom(thisdialog.get(0).roomId,thisdialog.find("[name=srTxtFrom]").val().getDate(),thisdialog.find("[name=srTxtTo]").val().getDate(),thisdialog.find("textarea").val());
					else EditDNRRoom(dnr.id,thisdialog.get(0).roomId,thisdialog.find("[name=srTxtFrom]").val().getDate(),thisdialog.find("[name=srTxtTo]").val().getDate(),thisdialog.find("textarea").val());
				}});
			buttons.push({ title:"Cancel", callback: function(thisdialog,args){
				thisdialog.dialog("close");
			}});
			var size = { width: 485};
			var dialog = customDialog(title,content,size,buttons,null);
			dialog.get(0).roomId = roomId;
			if(list!=null&&list.length>0) fillDNRDetail(dialog,list);
			ApplyCalendarDNRPopup(dialog,roomName,dnr);
		}
	})
}

function fillDNRDetail(dialog,list){
	dialog.get(0).DNRList = list;
	dialog.find("hr").css("display","");
	var detail = dialog.find("#DNRRoomDetail");
	detail.css("display","");
	
	var table = $("<table class='simpleTableStyle paddingLine' style='width:100%'><tr><td style='width:45%'>"+simpleLabel_From+"</td><td style='width:45%'>"+simpleLabel_To+"</td><td style='width:10%'>&nbsp;</td></tr></table>");
	for(var j=0;j<list.length;j++)
	{
		var f = "<tr dnr='{0}'><td>{1}</td><td>{2}</td><td>{3}&nbsp;&nbsp;{4}<td></tr>";
		table.append(f.format(
			list[j].id,
			new Date(list[j].date).getFormatedString(),
			new Date(list[j].dateTo).getFormatedString(),
			"<img src='/images/components/DNR/edit.png' class='link' title='view' onclick='editDNR(this)' />",
			(new Date(list[j].date) < getDate())? "&nbsp;" : "<img src='/images/components/DNR/fulltrash.png' class='link' title='delete' onclick='delDNR(this)' />"
		));		
	}
	dialog.parent().css("top","50%");
	dialog.parent().css("margin-top",dialog.height()*-1 + 100);
	detail.append(table);
}

function delDNR(el){
	confirmDialog("Are you sure to delete the DNR",function(el){
		var dnr = $(el).parents("tr:first").attr("dnr");
		$(el).parents("#BlankDialog").dialog("close")
		componentLoading($(".setright table"));
		simpleserviceWithError("/DNRService",{func:"DelDNR",dnr:dnr},function(data){
			closeComponentLoading($(".setright table"));
			cerltimespan(savedSpan);
		},function(data){
			closeComponentLoading($(".setright table"));
		});
	},el)
}

function editDNR(el){
	var dialog = $(el).parents("#BlankDialog:first");
	componentLoading(dialog);
	window.dnrDom = $(el);
	setTimeout(function(){
		var dom = window.dnrDom;
		delete window.dnrDom;
		
		var dialog = dom.parents("#BlankDialog:first");
		var id = dom.parents("tr:first").attr("dnr");
		var dnr = getObjectFromSharedData(dialog.get(0).DNRList,id);
		dialog.find("[name=srTxtFrom]").val(new Date(dnr.date).getFormatedString());
		dialog.find("[name=srTxtTo]").val(new Date(dnr.dateTo).getFormatedString());
		dialog.find("textarea").val(dnr.description);
		
		closeComponentLoading(dialog);
	},200);
}

function FillDNRRoomLabel(){
	$("#DNRRoom [name=label_from]").html(simpleLabel_From);
	$("#DNRRoom [name=label_to]").html(simpleLabel_To);
	$("#DNRRoom [name=label_comments]").html(simpleLabel_Comments);
}

function ApplyCalendarDNRPopup(dialog,roomName,dnr)
{
	if(dnr==null){
		dialog.find("[name=srTxtFrom]").val(getDate().getFormatedString());
		dialog.find("[name=srTxtTo]").val(getDate().addDate(1).getFormatedString());
	}
	else{
		dialog.find("[name=srTxtFrom]").val(new Date(dnr.date).getFormatedString());
		dialog.find("[name=srTxtTo]").val(new Date(dnr.dateTo).getFormatedString());
		dialog.find("textarea").val(dnr.description);
	}
	dialog.find("[name=DNR_from]").click(function(e){
		var dialog = $(e.srcElement).parents("#BlankDialog");
		var minDate = getDate();
    	SelectDateTimePicker(dialog.find("[name=srTxtFrom]").get(0),"bottom",minDate,null,function(val,dom,isChanged){
			if(!isChanged) return;
			var dialog = dom.parents("#BlankDialog");
			if(val.getDate() > dialog.find("[name=srTxtTo]").text().getDate())
				dialog.find("[name=srTxtTo]").val(val.getDate().addDate(1).getFormatedString());
		},-100,0);
	});
	dialog.find("[name=DNR_to]").click(function(e){
		var dialog = $(e.srcElement).parents("#BlankDialog");
		var minDate = dialog.find("[name=srTxtFrom]").val().getDate().addDate(1);
    	SelectDateTimePicker(dialog.find("[name=srTxtTo]").get(0),"bottom",minDate,null,function(val,dom,isChanged){},-100,0);
	});
}

function SaveDNRRoom(roomId,dateFrom,dateTo,desc)
{
	var datefrom = dateFrom.toSystemFormat();
	var dateto = dateTo.toSystemFormat();
	componentLoading($(".setright table"));
	simpleserviceWithError("/DNRService",{func:"AddDNR",roomId:roomId,datefrom:datefrom,dateto:dateto,desc:desc},function(data){
		closeComponentLoading($(".setright table"));
		var objArray = JSON.parse(data);
		if(objArray == null) simpleDialog(saveFail_label);
		cerltimespan(savedSpan);
	},function(data){
		closeComponentLoading($(".setright table"));
	});
}

function EditDNRRoom(id,roomId,dateFrom,dateTo,desc)
{
	var datefrom = dateFrom.toSystemFormat();
	var dateto = dateTo.toSystemFormat();
	componentLoading($(".setright table"));
	simpleserviceWithError("/DNRService",{func:"UpdateDNR",dnrId:id,roomId:roomId,datefrom:datefrom,dateto:dateto,desc:desc},function(data){
		closeComponentLoading($(".setright table"));
		var objArray = JSON.parse(data);
		if(objArray == null) simpleDialog(saveFail_label);
		else cerltimespan(savedSpan);
	},function(data){
		closeComponentLoading($(".setright table"));
	});
}

function showDNRDialog(dom,_roomid,_date){
	var dnrId = dom.attr("dnr");
	var roomid = (_roomid==null)?dom.parents("tr:first").attr("roomname"):_roomid;
	var date = $.datepicker.parseDate('yy-mm-dd',(_date==null)?dom.parents("td:first").attr("date"):_date);
	var data = {func:"GetRoomDNR",roomid:roomid};
	if(date < getDate())
		data.dnr = dom.attr("dnr");
	simpleserviceWithArgs("/DNRService",data,function(data,dnrId){
		var objArray = JSON.parse(data);
		var dnr = getObjectFromSharedData(objArray.List,dnrId);
		if(objArray.DNR != null)
			dnr = objArray.DNR;
		PopupDNR(dnr.roomId,getObjectFromSharedData(SharedData.Room,dnr.roomId).roomName,dnr,objArray.List);
	},dnrId);
}